//
//  ST_DTVideoCell.swift
//  mewpoo
//
//  Created by DouLei on 2018/12/27.
//  Copyright © 2018 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift


class ST_DTVideoCell: ST_DTChatCell {
    
    let playImageViewWidth:CGFloat = 44
    
    lazy var play_image_view:UIImageView = {
        
        let control = UIImageView()
        
        control.image = ST_ImageUtils.imageName("n_icon_全屏播放")
        
        
        return control
    }()

    fileprivate lazy var chat_image_view:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.add_touch_up(insideTarget: self, action: #selector(play_video))
        //        control.backgroundColor = Color.random
        control.cornerRadius = 15
        control.imageView?.contentMode = .scaleAspectFill
        return control
    }()
    
    
    
    
    @objc func play_video(){
        
        

        if let vc = self.parentViewController as? ST_PrivateChatViewController{
            vc.dismissMessageToolBar()
        }
        
        
        let playCtr = ST_BTPlayerViewController()
        playCtr.show_delete_action = false
        
        if FileManager.default.fileExists(atPath: message.videoLocalPath)  {
            
            playCtr.type = .localUrl
            
            playCtr.videoUrl =  message.videoLocalPath
            
        }else{
            
            playCtr.type = .networkUrl
            
            playCtr.videoUrl =  message.videoUrl
        }
        
        GlobalPresent(toController: playCtr)
    }
    
    fileprivate  lazy var uploading_mas_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.gray
        control.alpha = 0.8
        control.cornerRadius = 15
        control.isHidden = true
        return control
    }()
    
    fileprivate  lazy var indicator:UIActivityIndicatorView = {
        
        let control = UIActivityIndicatorView()
        
        return control
        
    }()
    fileprivate  lazy var indicator_label:UILabel = {
        
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 12)
        control.textAlignment = .center
        return control
    }()
    
    
    fileprivate lazy var close_mask_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0x373B40, transparency: 0.9)
        control.isHidden = true
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var close_status_icon_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("snapChat_close_icon"))
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var close_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "视频违规"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    override func commonInit(){
    
        super.commonInit()
        
        
        self.contentView.addSubview(chat_image_view)
        
        self.contentView.addSubview(uploading_mas_view)
        
        self.contentView.addSubview(play_image_view)
        
        
        self.uploading_mas_view.addSubview(indicator)
        self.uploading_mas_view.addSubview(indicator_label)
        
        play_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(chat_image_view)
            make.width.height.equalTo(playImageViewWidth)
        }
        
        
        indicator.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.center.equalToSuperview()
        }
        
        indicator_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(indicator.snp.bottom).offset(-20+5)
        }
        
        contentView.addSubview(close_mask_view)
        close_mask_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
        close_mask_view.addSubview(close_status_icon_view)
        close_status_icon_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(43)
            make.width.height.equalTo(30)
        }
        
        
        close_mask_view.addSubview(close_title_label)
        close_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(78)
            make.height.equalTo(20)
        }
    }
    
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            
            chat_image_view.snp.updateConstraints { (make) in
                make.size.equalTo(message.imageFrame.size)
            }
            
            
            if let check = message.passedCheck ,check == false{
                
                close_mask_view.isHidden = false
                
                chat_image_view.sd_setAllImage(with: "", placeholderImageName: nil)
                
                chat_image_view.imageView?.image = nil
                
            }else{
                
                close_mask_view.isHidden = true
                
                
                icon.sd_setImage(with: message.portraitUri.compress_image().url,for: UIControl.State.normal, completed: nil)
                
                
                if let image = message.videoThumbnailPath.load_image_from_disk(){
                    
                    chat_image_view.setImageForAllStates(image)
                    
                }else{
                    
                    chat_image_view.sd_setAllImage(with: message.videoThumbnailImageUrl)
                }
                
            }
            
            
                
            
        }
    }
    
    
    
    
    override func setting_uplaod_status(){
        
        if message.uploadStatus == .success{
            uploading_mas_view.isHidden = true
            
            indicator.stopAnimating()
            
            play_image_view.isHidden = false
            //                    print("A9999success")
        }else if message.uploadStatus == .failure{
            
            play_image_view.isHidden = true
            
            uploading_mas_view.isHidden = true
            
            
            indicator.stopAnimating()
            
        }else{
            
            play_image_view.isHidden = true
            
            
            uploading_mas_view.isHidden = false
            
            indicator.startAnimating()
            
            indicator_label.text = "\(message.uploadImageProgress)%"
            
        }
        
    }

}




//MARK: - LeftCell
class ST_DTVideoLeftCell: ST_DTVideoCell {
    
    override func commonInit() {
        super.commonInit()
        
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(15)
        }
        
        chat_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalTo(icon)
        }
      
        uploading_mas_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
//        upload_error_btn.snp.makeConstraints { (make) in
//            make.left.equalTo(chat_image_view.snp.right).offset(10)
//            make.centerY.equalTo(chat_image_view)
//            make.width.height.equalTo(ST_DTChatCell.uploadErrorBtnSize)
//        }
        
        self.contentView.addSubview(tailView)
        tailView.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.top.equalTo(chat_image_view.snp.bottom).offset(5)
            make.left.equalTo(chat_image_view)
        }
        
    }
    
    fileprivate lazy var tailView:ST_ChatMessageTailControl = {
         let control = ST_ChatMessageTailControl()
          return control
      }()
    
    override var message: ST_DTChatMessage!{
        didSet{
         
            tailView.model = self.message.tail
            
        }
    }
}



//MARK:-Right Cell
class ST_DTVideoRightCell: ST_DTVideoCell {
    
    override func commonInit() {
        super.commonInit()
        
        icon.removeFromSuperview()
        
        chat_image_view.snp.makeConstraints { (make) in
            make.height.equalTo(100)
            make.width.equalTo(100)
            make.right.equalToSuperview().offset(-30)
            make.top.equalToSuperview().offset(15)
        }
        
        uploading_mas_view.snp.makeConstraints { (make) in
            make.edges.equalTo(chat_image_view)
        }
        
        contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.right.equalTo(chat_image_view.snp.left).offset(-10)
            make.centerY.equalTo(chat_image_view)
            make.width.height.equalTo(ST_DTChatCell.uploadErrorBtnSize)
        }
        
        
        
        contentView.addSubview(uploadIndicator)
        uploadIndicator.snp.makeConstraints { (make) in
            make.centerY.equalTo(chat_image_view)
            make.right.equalTo(chat_image_view.snp.left).offset(-8)
        }
    }
    
   
    override func setting_uplaod_status() {
        
        super.setting_uplaod_status()
        
        if message.uploadStatus == .uploading {
            
            uploadIndicator.startAnimating()
            
        }else{
            uploadIndicator.stopAnimating()
        }
        
        upload_error_btn.isHidden = message.uploadStatus != .failure
    }
}
